/** @file    ManDocs.cpp
 *  @author  Alessandro Polo
 *  @version $Id: ManDocs.cpp 2556 2010-05-26 23:16:48Z alex $
 *  @brief
 * File containing methods for Man-related classes (as wosh::xtended::ManPage).
 * The header for this class can be found in ManDocs.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/xtended/ManDocs.h>

 #include <core/ObjectAllocator.h>


using namespace std;
namespace wosh {
 namespace xtended {

 WOSH_REGISTER(wosh::xtended::ManItem, "wosh::ISerializable", 1.0, _static_ManItem )
 WOSH_REGISTER(wosh::xtended::ManItemProperty, "wosh::xtended::ManItem", 1.0, _static_ManItemProperty )
 WOSH_REGISTER(wosh::xtended::ManSection, "wosh::xtended::ManItem", 1.0, _static_ManSection )
 WOSH_REGISTER(wosh::xtended::ManPage, "wosh::xtended::ManItem", 1.0, _static_ManPage )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////// ManItem

ManItem& ManItem::operator=(const ManItem& m) {
	if (this == &m) return *this; // same object?

	this->name = m.name;
	this->description = m.description;
	this->references = m.references;
	this->keywords = m.keywords;

	return *this;
 }

bool ManItem::operator==(const ManItem &other) const {
	if ( this->name != other.name )
		return false;

	return true;
 }

std::string ManItem::toString() const {
	string buffer;
	buffer = this->name + "\n============================\n";
	if ( !this->description.empty() )
		buffer += this->description + "\n";

	if ( !this->references.empty() ) {
		buffer += "\nReferences:\n";
		vector<ManReference>::const_iterator it;
		for( it=this->references.begin(); it!=this->references.end(); ++it ) {
			buffer += Utilities::format(" - %s [%s]\n", (*it).name.c_str(), (*it).type.c_str() );
		 }
	 }
	if ( !this->keywords.empty() ) {
		buffer += "\nKeywords: ";
		vector<ManKeyword>::const_iterator it;
		for( it=this->keywords.begin(); it!=this->keywords.end(); ++it ) {
			buffer += (*it).name + ";";
		 }
	 }
	return buffer;
 }

bool ManItem::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ret = hash_context->update( this->name ) & ret;
	ret = hash_context->update( this->description ) & ret;
	return ret;
 }

///////////////////////////////////////////////////////////////////////////////////////////////////// ManItem
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////// ManItemProperty

std::string ManItemProperty::toString() const
 {
	string buffer;
	buffer = this->name + "\n============================\n";
	if ( !this->description.empty() )
		buffer += this->description + "\n";

	if ( !this->valueDefault.isEmpty() ) {
		buffer += "\nValue:";
		if ( !this->valueDefault.isEmpty() )
			buffer += Utilities::format("Default '%s'", this->valueDefault.toString().c_str() );
		buffer += Utilities::format(", type=%s", this->valueDefault.getTypeAsString() );
	 }

	if ( !this->references.empty() ) {
		buffer += "\nReferences:\n";
		vector<ManReference>::const_iterator it;
		for( it=this->references.begin(); it!=this->references.end(); ++it ) {
			buffer += Utilities::format(" - %s [%s]\n", (*it).name.c_str(), (*it).type.c_str() );
		 }
	 }
	if ( !this->keywords.empty() ) {
		buffer += "\nKeywords: ";
		vector<ManKeyword>::const_iterator it;
		for( it=this->keywords.begin(); it!=this->keywords.end(); ++it ) {
			buffer += (*it).name + ";";
		 }
	 }
	return buffer;
 }

///////////////////////////////////////////////////////////////////////////////////////////// ManItemProperty
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////// ManSection

std::string ManSection::toString() const
 {
	string buffer = ManItem::toString();
	tStrManItemMap::ConstIterator it;
	const tStrManItemMap::ConstIterator it_end = this->items.end();
	for( it=this->items.begin(); it!=it_end; ++it ) {
		if ( it->second == NULL ) continue;
		buffer += it->second->toString() + "\n";
	 }
	return buffer;
 }

////////////////////////////////////////////////////////////////////////////////////////////////// ManSection
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////// ManPage

std::string ManPage::toString() const
 {
	string buffer = ManItem::toString();
	tStrManSectionMap::ConstIterator it;
	const tStrManSectionMap::ConstIterator it_end = this->sections.end();
	for( it=this->sections.begin(); it!=it_end; ++it ) {
		buffer += it->second->toString() + "\n";
	 }
	return buffer;
 }

///////////////////////////////////////////////////////////////////////////////////////////////////// ManPage
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace xtended
}; // namespace wosh
